<?php
/*
 * File: ActivityLog.php
 * Created Time: 2024-05-29 11:27:52 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-15 3:24:59 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2024 成都睿創科技有限公司
 */



namespace App\Models;

use DateTimeInterface;
use App\Models\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\ActivityLog
 *
 */
class ActivityLog extends Base{

    protected $table = 'activity_log';


/*********************************************************全局作用域注册**********************************************************************/
    protected static function boot(){
       parent::boot();

	   static::addGlobalScope('activity_status', function (Builder $builder) {
		  return $builder->where('activity_log.activity_status', '<>', -1);
	   });
    }


    /**
     * 函數構造
     */
    public function __construct(){
        $this->language = app('global_language') == "en" ? "en_" : "";
    }



/**********************************************************admin**********************************************************************/

    /**
     * 獲取員工-操作日誌
     * @param $data
     * @return array
     */
    public function getActivityLogListWeb($data){
        $limit = $data['limit'] ?? 10;
        $page = ($data['page'] ?? 1) - 1;
        $offset = $page * $limit;
        $query = ActivityLog::where('activity_status', 1);
        if (!empty($data['activity_type']) && $data['activity_type'] !== '' && $data['activity_type'] !== null) {
            // 条件
            $query->where('activity_type', $data['activity_type']);
        } else {
            // 默认
            $query->whereIn('activity_type', [1, 2, 3, 4, 5, 9]);
        }
        if (!empty($data['admin_name'])) {
            $query->where('admin_name', 'like', '%' . $data['admin_name'] . '%');
        }
        if (!empty($data['time'])) {
            $query->whereBetween('created_at', [$data['time'][0], $data['time'][1]]);
        }
        $datas['data'] = $query->clone()
            ->offset($offset)
            ->limit($limit)
            ->latest()
            ->get()
            ->toArray();
        $datas['count'] = $query->clone()
            ->whereIn('activity_type', [1, 2, 3, 4, 5, 9])
            ->count();
        return $this->success(__('web.success'),$datas);
    }

    /**
     * 獲取員工-訂單操作日誌
     * @param $data
     * @return array
     */
    public function getOrderActivityLogListWeb($data){
        $limit = empty($data['limit']) ? 10 : $data['limit'];
        $page =  ( $data['page'] - 1 ) * $limit;
        $time = null;
        $where[] = array('activity_type','>=',6);//6.訂單修改預約時間 7.訂單確認支付 8.訂單確認完成/確認收貨 9.訂單刪除(需要在操作日誌查看)
        $where[] = array('activity_status','=',1);
        if (!empty($data['admin_name'])) {
            $where[] = array('admin_name', 'like', '%' . $data['admin_name'] . '%');
        }
        if (isset($data['activity_type']) && (!empty($data['activity_type']) || is_numeric($data['activity_type']))) {
            $where['activity_type'] = $data['activity_type'];
        }
        if(!empty($data['time'])){
            $time['start_time'] = $data['time'][0];
            $time['end_time'] = $data['time'][1];
        }
        $datas['data']= $this->where($where)
            ->where(function ($query) use ($time){
                if($time != null){
                    $query->whereBetween('created_at',$time);
                }
            })
            ->where('order_id','=',$data['order_id'])//篩選本訂單的操作記錄
            ->offset($page)
            ->limit($limit)
            ->latest()
            ->get()->toArray();
        $datas['count'] = $this->where($where)
            ->where(function ($query) use ($time){
                if($time != null){
                    $query->whereBetween('created_at',$time);
                }
            })
            ->where('order_id','=',$data['order_id'])//只統計該訂單的操作記錄
            ->count();
        return $this->success(__('web.success'),$datas);
    }


    /**
     * 刪除員工操作日誌
     * @param $activityID
     * @return array
     */
    public function deleteActivityLogWeb($activityID){
        $data = self::softDelete('activity_log','activity_id',$activityID,'activity_status',-1);
        if($data){
            return $this->success(__('web.delete_success'));
        }else{
            return $this->error(__('web.delete_fail'));
        }
    }


/**********************************************************api**********************************************************************/


/**************************************************common(api和admin共用)***********************************************************/

}
